package edu.dayu.demo3;

/**
 * 给出 左上角和右下角的坐标，然后形成一个 子矩阵，计算子矩阵的和。
 */
public class 二维子矩阵的和 {
    public static void main(String[] args) {
        int[][] matrix = {{3,0,1,4,2},{5,6,3,2,1},{1,2,0,1,5},{4,1,0,1,7},{1,0,3,0,5}};
        NumMatrix nums = new NumMatrix(matrix);
        System.out.println(nums.sumRegion(2, 1, 4, 3));
    }

    static class NumMatrix {
        int[][] matrix;

        public NumMatrix(int[][] matrix) {
            this.matrix = matrix;
        }

        public int sumRegion(int row1, int col1, int row2, int col2) {
            int sum = 0;
            for (int i = row1; i <= row2 ; i++) {
                for (int j = col1; j <= col2; j++) {
                    sum+= matrix[i][j];
                }
            }
            return sum;
        }
    }
}
